<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends Www_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
    }

    /**
     * 首页
     */
    public function index()
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc']     = $this->banner_m->select(['type' => 0]);
        $banner['mobile'] = $this->banner_m->select(['type' => 1]);
        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        if ($banner['mobile']) {
            foreach ($banner['mobile'] as $key => $val) {
                $banner['mobile'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['mobile'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['mobile'][$key]['pic_url']);
            }
        }
        // 热销产品
        $this->load->model('article_m');
        $hot = $this->article_m->select(['is_hot' => 1], 4, 0);
        // 最新产品
        $product = $this->article_m->select("", 8, 0, "id,title,keyword,img");
        // 新闻
        $this->load->model('account_m');
        $news            = $this->account_m->select("", 6, 0, "id,title,keyword,describe,create_time,img");
        $data['news']    = $news;
        $data['product'] = $product;
        $data['hot']     = $hot;
        $data['banner']  = $banner;
        $this->load->view('index', $data);
    }

    /**
     * 柔美
     */
    public function page($id = 1)
    {
        // 获取简介
        $this->load->model('page_m');
        $page         = $this->page_m->find($id);
        $data['page'] = $page ?? "";
        $data['rid']  = $id;

        $this->load->view('about', $data);
    }

    /**
     * 关于我们
     */
    public function contact()
    {
        $data['rid'] = 5;
        $this->load->view('contact', $data);
    }

    public function jobs()
    {
        $data['rid'] = 5;
        $this->load->view('jobs', $data);
    }

    /**
     * 加入我们
     */
    public function join($id = 5)
    {
        // 获取简介
        $this->load->model('page_m');
        $page         = $this->page_m->find($id);
        $data['page'] = $page ?? "";
        $data['id']  = $id;
        $this->load->view('join', $data);
    }

    /**
     * 皮肤护理
     */
    public function product($id = 0, $page = 1)
    {

        $this->load->model('article_m');
        $where = [];
        if ($id) {
            $where = ['category' => $id];
        }
        //分页
        $limit                      = 8;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->article_m->total($where);
        $config['base_url']         = base_url('product/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $products           = $this->article_m->select($where, $limit, $start);

        $data["products"] = $products ?? [];
        $data["id"]       = $id;
        // pp($data);exit();
        $this->load->view('product', $data);
    }

    /**
     * 资讯详情
     */
    public function product_show($id = 0)
    {
        $this->load->model('article_m');
        $this->load->model('articleCategory_m');
        $article         = $this->article_m->find($id);
        $data["article"] = $article ?? [];
        // 分类
        $cur_category         = $this->articleCategory_m->find($article['category']);
        $data["cur_category"] = $cur_category ?? [];
        //获取上一篇
        // $data["pre_article"]  = $this->account_m->findBy("id < {$id}");
        // $data['next_article'] = $this->account_m->findBy("id > {$id}");
        // pp($data);exit();
        $this->load->view('product_show', $data);
    }

    /**
     * 合作流程
     */
    public function price()
    {
        $this->load->view('price');
    }

    /**
     * 直通资讯
     */
    public function news($id = 1, $page = 1)
    {
        $where = ['deletetd' => 0];
        if ($id) {
            $where['category'] = $id;
        }
        $this->load->model('account_m');
        $this->load->library('pagination');
        $limit                      = 6;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->account_m->total($where);
        $config['base_url']         = base_url('news/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $field              = "id,category,title,describe,create_time,img";
        $data["news"]       = $this->account_m->select($where, $limit, $start, $field);
        $data["total"]      = $config['total_rows'];
        $data["id"]         = $id;

        $this->load->view('news', $data);
    }

    /**
     * 资讯详情
     */
    public function news_show($id = 0)
    {
        $this->load->model('account_m');
        $article         = $this->account_m->find($id);
        $data["article"] = $article ?? [];
        // 分类
        $cur_category         = $this->zixunCategory_m->find($article['category']);
        $data["cur_category"] = $cur_category ?? [];
        // pp($data);exit();
        $this->load->view('news_show', $data);
    }

}
